package com;

import java.sql.*;

/*
   Class.forName("com.mysql.jdbc.Driver");
   String url = "jdbc:mysql://localhost:3306/DSSchool?useUnicode=true&characterEncoding=UTF-8&useSSL=false";

   Class.forName("com.mysql.cj.jdbc.Driver");
   String url = "jdbc:mysql://localhost:3306/DSSchool?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC";

*/


public class JdbcUtils {
    private static String url = "jdbc:mysql://localhost:3306/dsschool?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
    //private static String url = "jdbc:mysql://localhost:3306/dsschool?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC";
    //private static String url = "jdbc:mysql://localhost:3306/java_web?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC";
    private static String driverClass = "com.mysql.cj.jdbc.Driver";
    //private static String user = "java_web";
    private static String user = "root";
    private static String password = "root";
    //private static String password = "aXhrzh2R34hK7cWS";

    private static Connection conn = null;

    public static Connection getConnection() {
        try {
            Class.forName(driverClass);
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("数据库连接成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
        return conn;
    }


    public static void close(Statement stmt, Connection conn) {
        try {
            if (stmt != null) {
                stmt.close();
            }

            if (conn != null) {
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }


    public static void close(ResultSet rs, Statement stmt, Connection conn) {

        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

}